package cu.tedu._05mvcboot03.controller;

import cu.tedu._05mvcboot03.commom.response.JsonResult;
import cu.tedu._05mvcboot03.commom.response.StatusCode;
import cu.tedu._05mvcboot03.mapper.ArticleMapper;
import cu.tedu._05mvcboot03.mapper.MvcUserMapper;
import cu.tedu._05mvcboot03.pojo.dto.UserArticleDTO;
import cu.tedu._05mvcboot03.pojo.dto.UserLoginDTO;
import cu.tedu._05mvcboot03.pojo.entity.MvcArticle;
import cu.tedu._05mvcboot03.pojo.vo.ArticleVO;
import cu.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@Validated
@Api(tags = "01.用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private MvcUserMapper mvcUserMapper;
    @Autowired
    private ArticleMapper articleMapper;


    @ApiOperation(value = "登录功能")
    @PostMapping("login")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {

        log.debug("userLoginDTO:" + userLoginDTO);
        UserVO userVO = mvcUserMapper.selectUser(userLoginDTO.getUsername(), userLoginDTO.getPassword());

        if ((userVO != null) && userVO.getPassword().equals(userLoginDTO.getPassword())) {
            session.setAttribute("user", userVO);
            return new JsonResult(StatusCode.OPERATION_SUCCESS);
        } else {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
    }

    @ApiOperation(value = "发表文章功能")
    @PostMapping("article")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session) {
        log.debug("userArticleDTO:" + userArticleDTO);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        MvcArticle mvcArticle = new MvcArticle();
        BeanUtils.copyProperties(userArticleDTO, mvcArticle);
        mvcArticle.setUserId(userVO.getId());
        articleMapper.insert(mvcArticle);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    @ApiOperation(value = "查询指定文章功能")
    @ApiImplicitParam(name = "id", value = "文章编号", required = true, dataType = "int")
    @GetMapping("selectByArticleId")
    public JsonResult selectByArticleId(@Range(min = 1,max = 100,message = "文章ID必须在1-100之间") @RequestParam int id) {
        log.debug("id:" + id);
        ArticleVO articleVO = articleMapper.selectByArticleId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);
    }
}
